#!/bin/sh
set -x


echo **********************************Start********************************
date

# check version value 
echo $Version
# 检查版本号,如果版本号为0,提示错误，并退出
function checkVersion(){

	if [ "${Version}" -eq "0" ];then
    	echo "The current version number value is 0,exit 1"
        exit 1
    fi
}

checkVersion


cd ${WORKSPACE}/master
# 获取最近一次远程 master 提交的 commit id
sha1=`git rev-parse remotes/origin/master^{commit}`
# 获取姓名及邮箱，来配置git提交者信息
name=`git show --pretty=%an $sha1 | awk 'NR==1{print}'`
email=`git show --pretty=%ce $sha1 | awk 'NR==1{print}'`
echo '################# 当前提交人信息:'
echo $name 
echo $email 
git config --global user.name $name
git config --global user.email $email

#git remote add origin http://192.168.16.20/k8s-test/springboot-demo.git
 



echo ${Action}


remote="origin"
newBranch=${Raw_Branch}
target_Branch=${Target_Branch}
label_tag=${Label_tag}
label_content=${Label_content}



##检测版本号格式通过之后,根据构建时选择的动作执行封版或合代码动作
# 封版操作

function MergeBranch(){

  newBranch=${Raw_Branch}
  echo "Merge $newBranch into master Branch..."
  git checkout $target_Branch
  git merge origin/$newBranch || exit 1
  git push --set-upstream origin $target_Branch || exit 1
  echo "Merge into master branch successful ..."
  
  echo "create master label tag"
  # 创建TAG 标签
  git tag -a $label_tag -m "标签信息: ${label_content}"
  # 推送TAG 标签
  git push origin $label_tag
  echo '################# 当前创建的标签为:' $label_tag
  echo **********************************End**********************************

}


# 创建分支

function CreateBranch(){

  echo "switch to $newBranch branch..."
  git branch $newBranch
  
  echo "Create and toggle to $target_Branch branch..."
  git checkout -b $target_Branch
  git commit -a -m "add ${target_Branch}"
  git push --set-upstream origin ${target_Branch}

}


case $Action in
    CreateBranch)
    	CreateBranch
        ;;
    MergeBranch)
        MergeBranch
        ;;
esac